package nl.infodation.emr.pricingservice.ui;

import java.util.ArrayList;
import java.util.List;

import nl.infodation.emr.pricingservice.entity.DomainRule;
import nl.infodation.emr.pricingservice.ui.component.ExplicitPagingContainer;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Table;

public class PromotionCodeList extends Table {
	
	private static final long serialVersionUID = 1L;

	final private ExplicitPagingContainer pagingContainer = new ExplicitPagingContainer();
	
	public Object[] STANDARD_COL_NAME;

	public String[] STANDARD_COL_HEADER;

	private void initColumnData() {
		STANDARD_COL_NAME = new Object[] { "ruleId","ruleName" };

		STANDARD_COL_HEADER = new String[] {
				SessionData.getI18N().getMessage(Constants.RULE_ID_HEADER),
				SessionData.getI18N().getMessage(Constants.RULE_NAME_HEADER)};
	}
	
	public PromotionCodeList() {
		initColumnData();
		setSizeFull();
		initializePagingContainer();
		setContainerDataSource(pagingContainer);
		setPageSize(6);
		setVisibleColumns(STANDARD_COL_NAME);
		setColumnHeaders(STANDARD_COL_HEADER);		
		setColumnCollapsingAllowed(true);
		setColumnReorderingAllowed(true);
		setColumnProperties();
		setSelectable(true);
		setImmediate(true);
		setNullSelectionAllowed(false);
	}

	private void setColumnProperties() {
//		setColumnAlignment("value", Table.ALIGN_CENTER);
	}

	private void initializePagingContainer() {
		pagingContainer.addContainerProperty("ruleId", String.class, null);
		pagingContainer.addContainerProperty("ruleName", String.class, null);
	}

	private void setPageSize(int size) {
		pagingContainer.setPageSize(size);
	}
	
	public int getPageSize()
	{
		return pagingContainer.getPageSize();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setDataSource(List<DomainRule> rules) {
		List<BeanItem> items = new ArrayList<BeanItem>();
		for (DomainRule rule : rules) {
			items.add(new BeanItem(rule));
		}
		pagingContainer.setDataSource(items);
	}
	
}
